﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using eap.share;
using System.Text.RegularExpressions;
using System.Text;

namespace zsproject.page
{
	public class Sampling : System.Web.UI.Page
	{
		//存放要传递到aspx文件显示的数据
		public System.Collections.Hashtable output=new System.Collections.Hashtable();
		private string method="";
		public string this_year_id="";
		public string this_semester_id="";
		public string this_class_id="";
		public string this_course_id="";
		public string this_type="";

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			//---------------------------------------------------------------
			Response.AddHeader("Pragma","No-cache");
			Response.AddHeader("Cache-Control", "no-cache");
			Response.Expires=0;
			//---------------------------------------------------------------

			//读取当前方法
			string page=Req.getPara(Request,"page");
			this_year_id=Req.getPara(Request,"this_year_id");
			this_semester_id=Req.getPara(Request,"this_semester_id");
			this_class_id=Req.getPara(Request,"this_class_id");
			this_course_id=Req.getPara(Request,"this_course_id");

			//初始化系统(基本参数，配置参数，数据库连接参数)
			AdminLib.init(Request,Response,Server);
			//检查用户是否登录，是否有权限
			AdminUser user = (AdminUser)Session["_user"];

			if(user==null || !user.isLogin()){ Response.Redirect("../admin/index.aspx?page=unlogin"); Response.End() ; }
			if(user.isRelogin(Request)){ Response.Redirect("../admin/index.aspx?page=relogin"); Response.End() ; }
			//没有菜单的,特殊链接可以不要
			if(!user.getRight(Request)){ Response.Redirect("../admin/index.aspx?page=erraccess"); Response.End() ; }
			if(!user.isWarning()){ Response.Redirect("../admin/index.aspx?page=warning"); Response.End(); }
			//资源权限
			output.Add("actlist",DAO.sqlValueList("SELECT * FROM  dbo.getRightId("+user["id"]+")")); //保存用户有权限的操作	

			
			if(page=="Form") Form(Request,Response,Session,output);
			if(page=="Form_action"){Form_action(Request,Response,Session,output);}
		}

	 
		private void Form(System.Web.HttpRequest Request, System.Web.HttpResponse Response,System.Web.SessionState.HttpSessionState Session,System.Collections.Hashtable output)
		{
			AdminUser user = (AdminUser)Session["_user"];
			
			string d=Sys.toSQLText(Req.getPara(Request,"d"));
			if(d!="") d=""+Sys.toInteger(d);

			System.Data.DataRow map=DAO.sqlRow("select * from t_timetable where id ='"+d+"'");			


			string list_id=Sys.toSQLText(Req.getPara(Request,"list_id"));
			System.Data.DataRow map_list=DAO.sqlRow("select * from t_timetable_list where id ='"+list_id+"'");		
			/*-------------------------------------附件信息---------------------------------------*/
			/*
			if(map!=null && map["accessory_ids"]!=null && map["accessory_ids"].ToString()!="")
			{
				string sql="select * from tbzs_accessory where id in("+map["accessory_ids"]+")";
				DataRowCollection listAtt = DAO.sqlList(sql);//附件信息
				output.Add("listAtt",listAtt);
			}
			*/
			/*-------------------------------------附件信息---------------------------------------*/
			//保存结果
			output.Add("map",map);		 
			output.Add("map_list",map_list);		 
		}//end method 

		private void Form_action(System.Web.HttpRequest Request, System.Web.HttpResponse Response,System.Web.SessionState.HttpSessionState Session,System.Collections.Hashtable output)
		{
			AdminUser user = (AdminUser)Session["_user"];
			string sql = "",message="",strMenu="信息";
			string errmsg="";
			int errcode=0;
			string action=Req.getPara(Request,"action");

			string d=Sys.toSQLText(Req.getPara(Request,"d"));
			if(d!="") d=""+Sys.toInteger(d);
			//获取页面参数
			string id=Sys.toSQLText(Req.getPara(Request,"id"));
			
			this_year_id=Req.getPara(Request,"this_year_id");
			this_semester_id=Req.getPara(Request,"this_semester_id");
			this_class_id=Req.getPara(Request,"this_class_id");
			this_course_id=Req.getPara(Request,"this_course_id");

			
			string t_specialty_id=Sys.toSQLText(Req.getPara(Request,"t_specialty_id"));
			string sample_day=Sys.toSQLText(Req.getPara(Request,"id"));
			string sample_sum=Sys.toSQLText(Req.getPara(Request,"id"));
			string sample_num=Sys.toSQLText(Req.getPara(Request,"id"));
			string score_type=Sys.toSQLText(Req.getPara(Request,"id"));


			//检查输入的内容
			//if(title=="") errmsg=errmsg+"标题不能为空。";
			if(errmsg!=""){ Response.Write(user.getActionAlarm(errmsg)); return; }		
		
			
			if(action=="save")
			{
				sql="select count(*) from t_student where t_class_id='"+this_class_id+"'";
				int student_sum=int.Parse(DAO.sqlValue(sql));

				sample_sum=student_sum+"";
				sample_num=System.Math.Ceiling(student_sum*0.1)+"";


				sql="select count(*) from t_student where t_class_id='"+this_class_id+"'";

				if(d=="")
				{
					//新增
					sql=@"INSERT INTO t_sampling
					(						
						[t_year_id] ,
						[t_semester_id] ,
						[t_specialty_id] ,
						[t_course_id] ,
						[t_class_id] ,
						[sample_day] ,
						[sample_sum] ,
						[sample_num] ,
						[score_type] 
					)
					VALUES
					(
						'"+this_year_id+
						"','"+this_semester_id+
						"','"+t_specialty_id+
						"','"+this_course_id+
						"','"+this_class_id+
						"','"+sample_day+
						"','"+sample_sum+
						"','"+sample_num+
						"','"+score_type+
						"');Select @@IDENTITY";
					d = DAO.sqlValue(sql).ToString();

					string cond=String.Format("t_year_id='{0}' and t_semester_id='{1}' and t_class_id='{2}' and t_course_id='{3}' ",this_year_id,this_semester_id,this_class_id,this_course_id);

					if(d!=""){
						sql=string.Format("INSERT INTO t_sampling_list([t_sampling_id] ,[t_score_id]) Select TOP {0} '{1}',id From t_score where {2} Order By NewID() ",sample_num,d,cond);
						errcode=DAO.sqlExec(sql);
					}

					strMenu=strMenu+"(d="+d+")抽样";
				} 
				
				message=strMenu+(errcode==0?"成功":"出错")+"。";
			
			}		
	



			//Response.Write("<script>alert('"+message+"');</script>");
			if(errcode!=0) message=message+"(errcode="+errcode+")";
			if(errcode!=0) user.saveLog(Request,message); //保存日志
			string reload=errcode==0?Req.getPara(Request,"reload").Replace("{$id}",d):null; //如果发生了错误，则不需要重新刷新
			Response.Write(user.getActionString(Session,errcode,reload,message)); //显示结果 
		} 

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
